package com.sls.web.manager.action.recharge;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;

import com.sls.web.manager.action.SuperAction;
import com.sls.web.manager.common.WebTools;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.recharge.pojo.PriceTemplate;
import com.sls.web.manager.recharge.querybean.PriceTemplateQuery;
import com.sls.web.manager.recharge.service.PriceTemplateService;

/**
 * @author sxf
 * 
 */
public class PriceTemplateAction extends SuperAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = -7391079224287594188L;
	@Resource(name = "priceTemplateService")
	private PriceTemplateService priceTemplateService;
	private Pagination<PriceTemplate> page;
	private PriceTemplateQuery ptq;
	private PriceTemplate pt;
	private String id;

	public String list() throws Exception {
		page = priceTemplateService.findPage(ptq, getCurrentOperator());
		return "list";
	}

	/**
	 * 编辑价格模版
	 * 
	 * @param page
	 */
	public String toEdit() throws Exception {
		pt = priceTemplateService.findById(id, getCurrentOperator());
		return "edit";
	}

	public String doEdit() throws Exception {
		String id = WebTools.getParameter("id");
		String name = WebTools.getParameter("name");
		String result = "error";
		try {
			if (StringUtils.isNotBlank(id)) {
				PriceTemplate ptemp = new PriceTemplate();
				ptemp.setId(id);
				if (ptemp != null) {
					ptemp.setName(name);
				}
				priceTemplateService.update(ptemp, getCurrentOperator());
				result = "success";
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = "error";
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", result);
		map.put("id", "");
		// WebTools.writeAjax(map);
		message = WebTools.writeAjax(map);
		return "ajax";
	}

	public String doAdd() throws Exception {
		if (pt != null && StringUtils.isNotBlank(pt.getName())) {
			pt.setId(UUID.randomUUID().toString());
			pt.setEnable(true);
			pt.setDeleted(false);
			priceTemplateService.save(pt, getCurrentOperator());
		}
		return list();
	}

	public void setPage(Pagination<PriceTemplate> page) {
		this.page = page;
	}

	public Pagination<PriceTemplate> getPage() {
		return page;
	}

	public PriceTemplateQuery getPtq() {
		return ptq;
	}

	public void setPtq(PriceTemplateQuery ptq) {
		this.ptq = ptq;
	}

	public void setPt(PriceTemplate pt) {
		this.pt = pt;
	}

	public PriceTemplate getPt() {
		return pt;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

}
